package com.fqgj.sentry.manage;

import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.manage.dao.McAlarmManDao;
import com.fqgj.sentry.manage.entity.McAlarmManEntity;
import com.fqgj.sentry.manage.service.ConfigService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-16 下午12:09
 */

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private McAlarmManDao alarmManDao;

    @Override
    public McAlarmManEntity insertAlarmMan(McAlarmManEntity alarmManEntity) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("merchantId", alarmManEntity.getMerchantId());
        param.put("name", alarmManEntity.getName());
        McAlarmManEntity entity = alarmManDao.selectByNameAndMerchant(param);
        if (null != entity) {
            throw new BizException(ErrorCodeEnums.alarm_man_is_exist_error, "姓名已经存在");
        }
        return alarmManDao.insert(alarmManEntity);
    }

    @Override
    public Boolean deleteAlarmMan(Long merchantId, Long alarmManId) {
        McAlarmManEntity alarmManEntity = alarmManDao.selectByPrimaryKey(alarmManId);
        if (null == alarmManEntity || alarmManEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.alarm_man_not_exist_error);
        }
        if (!alarmManEntity.getMerchantId().equals(merchantId)) {
            throw new BizException(ErrorCodeEnums.merchant_alarm_man);
        }
        alarmManDao.deleteByPrimaryKey(alarmManId);
        return true;
    }

    @Override
    public Boolean updateAlarmMan(McAlarmManEntity alarmManEntity) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("merchantId", alarmManEntity.getMerchantId());
        param.put("name", alarmManEntity.getName());
        param.put("id",alarmManEntity.getId());
        McAlarmManEntity entity = alarmManDao.selectByNameAndMerchant(param);
        if (null != entity) {
            throw new BizException(ErrorCodeEnums.alarm_man_is_exist_error, "姓名已经存在");
        }
        alarmManDao.updateByPrimaryKey(alarmManEntity);
        return true;
    }

    @Override
    public McAlarmManEntity selectAlarmMan( Long alarmManId) {
        McAlarmManEntity alarmManEntity = alarmManDao.selectByPrimaryKey(alarmManId);
        if (null == alarmManEntity || alarmManEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.alarm_man_not_exist_error);
        }
        return alarmManEntity;
    }

    @Override
    public Page<McAlarmManEntity> selectAlarmManForPage(Long merchantId, String name, String email, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("merchantId", merchantId);
        param.put("name", name);
        param.put("email", email);
        Page<McAlarmManEntity> page = PageHelper.startPage(pageNum, pageSize);
        alarmManDao.selectAlarmManList(param);
        return page;
    }
}
